$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'FeedbackInfo/Select',
        datatype: "json",
        colModel: [
            { label: '反馈id', name: 'id', width: 40,key: true,hidden:true },
             { label: '标题', name: 'title', width: 60 },
             { label: '内容', name: 'content', width: 100 },
             { label: '反馈人', name: 'studentName'},
            { label: '班级', name: 'studentClassId' },
            { label: '学院', name: 'studentDepartment' },
             { label: '反馈时间', name: 'greateDate' },
            { label: '操作', name: '',width:60, sortable: false, formatter: function(value, options, row){

                var r='<span class="detail-btn" onclick="getDetails(\''+row.id+'\')">详情</span>'

                return r;
            }}
         ],
        viewrecords: true,
        height: '100%',
        rowNum: 10,
        rowList : [10,30,50],
        // rownumbers: true,
        // rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "scroll" });
        }
    });
    $.ajax({
        url: baseURL + "/college/getAllcollege",
        data: {},
        success: function (r) {
            $("#College").append("<option value=''>请选择学院</option>");
            for (i = 0; i < r.list.length; i++) {
                var tname = r.list[i].collegeName;
                var tid = r.list[i].collegeName;
                $("#College").append("<option value='" + tid + "'>" + tname + "</option>");
            }
        }
    });
    $("#College").change(function () {
        $("#Class").empty();
        var college=$("#College").val();
        $.ajax({
            url: baseURL + "/college/getClassByCollege",
            data: {
                college:college
            },
            success: function (r) {
                $("#Class").append("<option value=''>请选择班级</option>");
                for (i = 0; i < r.list.length; i++) {
                    var tname = r.list[i];
                    var tid = r.list[i];
                    $("#Class").append("<option value='" + tid + "'>" + tname + "</option>");
                }
            }
        });
        $("#Profession").empty();
        var college=$("#College").val();
        $.ajax({
            url: baseURL + "/college/getProfessionByCollege",
            data: {
                college:college
            },
            success: function (r) {
                $("#Profession").append("<option value=''>请选择专业</option>");
                for (i = 0; i < r.list.length; i++) {
                    var tname = r.list[i];
                    var tid = r.list[i];
                    $("#Profession").append("<option value='" + tid + "'>" + tname + "</option>");
                }
            }
        });
    });
    $("#Profession").change(function () {
        $("#Class").empty();
        var college=$("#College").val();
        var profession=$("#Profession").val();
        $.ajax({
            url: baseURL + "/college/getClassByCollege",
            data: {
                college:college,
                profession:profession
            },
            success: function (r) {
                $("#Class").append("<option value=''>请选择班级</option>");
                for (i = 0; i < r.list.length; i++) {
                    var tname = r.list[i];
                    var tid = r.list[i];
                    $("#Class").append("<option value='" + tid + "'>" + tname + "</option>");
                }
            }
        });
    });
});
function getDate(id) {
    var Nowdate=new Date();
    M=Number(Nowdate.getMonth())+1;
    var today=Nowdate.getFullYear()+"-"+M+"-"+Nowdate.getDate();
    if(id=='1'){
        $("#startDate").val(today);
        $("#endDate").val(today);
    }else if(id=='2'){
        $("#startDate").val(showWeekFirstDay());
        $("#endDate").val(today);
    }if(id=='3'){
        $("#startDate").val(showMonthFirstDay());
        $("#endDate").val(today);
    }
}
function showWeekFirstDay()
{
    var Nowdate=new Date();
    var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000);
    M=Number(WeekFirstDay.getMonth())+1
    return WeekFirstDay.getFullYear()+"-"+M+"-"+WeekFirstDay.getDate();
}
function showWeekLastDay()
{
    var Nowdate=new Date();
    var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000);
    var WeekLastDay=new Date((WeekFirstDay/1000+6*86400)*1000);
    M=Number(WeekLastDay.getMonth())+1
    return WeekLastDay.getFullYear()+"-"+M+"-"+WeekLastDay.getDate();
}
function showMonthFirstDay()
{
    var Nowdate=new Date();
    var MonthFirstDay=new Date(Nowdate.getFullYear(),Nowdate.getMonth(),1);
    M=Number(MonthFirstDay.getMonth())+1
    return MonthFirstDay.getFullYear()+"-"+M+"-"+MonthFirstDay.getDate();
}
function showMonthLastDay()
{
    var Nowdate=new Date();
    var MonthNextFirstDay=new Date(Nowdate.getFullYear(),Nowdate.getMonth()+1,1);
    var MonthLastDay=new Date(MonthNextFirstDay-86400000);
    M=Number(MonthLastDay.getMonth())+1
    return MonthLastDay.getFullYear()+"-"+M+"-"+MonthLastDay.getDate();
}
function  getDetails(id) {

    if(id == null){
        return ;
    }
    $.get(baseURL + "DbFile/Path/"+id, function(r){
        vm.showList = false;
        vm.title = "查看";
        vm.Dbfile2 = r.DbFile2;
        var images="";
        for(var i=0;i<vm.Dbfile2.length;i++)
        {
            images=images+"<img hidefocus=\"true\" src=\""+vm.Dbfile2[i]+"\" width=\"400\" height=\"300\">";
        }
        $("#images").html(images);
    });
    $.get(baseURL + "FeedbackInfo/Info/"+id, function(r){
        vm.showList = false;
        vm.title = "查看";
        vm.FeedbackInfo = r.FeedbackInfo;
    });
}
var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            key: null
        },
        showList: true,
        title: null,
        FeedbackInfo: {},
        Dbfile2: {},
        id: null
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.FeedbackInfo = {};
        },
        update: function () {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            $.get(baseURL + "DbFile/Path/"+id, function(r){
                vm.showList = false;
                vm.title = "查看";
                vm.Dbfile2 = r.DbFile2;
                var images="";
                for(var i=0;i<vm.Dbfile2.length;i++)
                {
                    images=images+"<img hidefocus=\"true\" src=\""+vm.Dbfile2[i]+"\" width=\"400\" height=\"300\">";
                }
                $("#images").html(images);
            });
            $.get(baseURL + "FeedbackInfo/Info/"+id, function(r){
                vm.showList = false;
                vm.title = "查看";
                vm.FeedbackInfo = r.FeedbackInfo;
            });
        },
        del: function () {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "FeedbackInfo/Delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function () {
            if(vm.validator()){
                return ;
            }

            var url = vm.FeedbackInfo.id == vm.id ? "FeedbackInfo/Update" : "FeedbackInfo/Insert";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.FeedbackInfo),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    'title': vm.q.key,
                    'startDate': $("#startDate").val(),
                    'endDate': $("#endDate").val(),
                    'college': $("#College").val(),
                    'class': $("#Class").val(),
                },
                page:page
            }).trigger("reloadGrid");
        },
        validator: function () {
            if(isBlank(vm.FeedbackInfo.id)){
                alert("公告id不能为空");
                return true;
            }
            if(isBlank(vm.FeedbackInfo.title)){
                alert("标题不能为空");
                return true;
            }
            if(isBlank(vm.FeedbackInfo.content)){
                alert("内容不能为空");
                return true;
            }
            if(isBlank(vm.FeedbackInfo.username)){
                alert("创建人不能为空");
                return true;
            }
            if(isBlank(vm.FeedbackInfo.greateDate)){
                alert("更新时间不能为空");
                return true;
            }
            if(isBlank(vm.FeedbackInfo.status)){
                alert("状态不能为空");
                return true;
            }
            }
        }
});